//
//  AuctionTreasureCollectionViewCell.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/4.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "AuctionTreasureCollectionViewCell.h"

@interface AuctionTreasureCollectionViewCell() {

}
/**
 价格
 */
@property (nonatomic, strong) UILabel *priceLabel;

/**
 时间
 */
@property (nonatomic, strong) UILabel *timeLabel;

/**
 已结束
 */
@property (nonatomic, strong) UILabel *endLabel;
@end
@implementation AuctionTreasureCollectionViewCell
@synthesize imageView = _imageView;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.layer.masksToBounds = true;
        self.layer.cornerRadius = 4.0;
        [self addObserver];
        [self initContentViews];
    }
    return self;
}

- (void)initContentViews {
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_equalTo(16);
    }];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_offset(2);
        make.right.mas_lessThanOrEqualTo(self).mas_offset(-10);
    }];
    [self.endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
}
- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNoti:) name:OYCountDownNotification object:nil];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -- mark 懒加载
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"logo")];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imageView];
    }
    return _imageView;
}
- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.font = SKFONT_SIZE(13);
        _priceLabel.textColor = [UIColor whiteColor];
        _priceLabel.text = @" ";
        [self addSubview:_priceLabel];
    }
    return _priceLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = SKFONT_SIZE(10);
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.layer.opacity = 0.8;
        _timeLabel.text = @" ";
        _timeLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_timeLabel];
    }
    return _timeLabel;
}
- (UILabel *)endLabel {
    if (!_endLabel) {
        _endLabel = [[UILabel alloc] init];
        _endLabel.text = @"已结束";
        _endLabel.textColor = [UIColor whiteColor];
        _endLabel.backgroundColor = SKColorWithHex(0x888888);
        _endLabel.layer.opacity = 0.8;
        _endLabel.font = SKFONT_SIZE(13);
        _endLabel.textAlignment = NSTextAlignmentCenter;
        _endLabel.hidden = true;
        [self addSubview:_endLabel];
    }
    return _endLabel;
}
#pragma mark -- mark 业务逻辑
- (void)setPrice:(NSString *)price {
    _price = price;
    self.priceLabel.text = [NSString stringWithFormat:@"¥ %@", price];
}

/// 倒计时
/// @param noti nodti
- (void)countDownNoti:(NSNotification *)noti {
    if (self.status == 1 && self.sinceStartTimeInterval > 0) {
        // 距离开始
        self.sinceStartTimeInterval -= 1;
        if (self.sinceStartTimeInterval <= 0) {
            // 结束 刷新数据
            if (self.countDownEndBlock) {
                self.countDownEndBlock(); }
        }else {
            int hour = self.sinceStartTimeInterval / 3600;
            int minute = (int)(self.sinceStartTimeInterval / 60) % 60;
            int second = (int)self.sinceStartTimeInterval % 60;
            self.timeLabel.text = [NSString stringWithFormat:@"距开始 %.2d : %.2d : %.2d", hour, minute, second];
        }
    }else if (self.status == 2 && self.sinceEndTimeInterval > 0) {
        // 距离结束
        self.sinceEndTimeInterval -= 1;
        if (self.sinceEndTimeInterval <= 0) {
            // 结束 刷新数据
            if (self.countDownEndBlock) {
                self.countDownEndBlock(); }
        }else {
            int hour = self.sinceEndTimeInterval / 3600;
            int minute = (int)(self.sinceEndTimeInterval / 60) % 60;
            int second = (int)self.sinceEndTimeInterval % 60;
            self.timeLabel.text = [NSString stringWithFormat:@"距结束 %.2d : %.2d : %.2d", hour, minute, second];
        }
    }
}


- (void)setStatus:(NSInteger)status {
    _status = status;
    switch (status) {
        case 1:
            //未开始
            [self readyStart];
            return;
        case 2:
            //进行中
            [self start];
            return;
        case 3:
            //已结束
            [self end];
            return;
        case 4:
            //已成交
            [self end];
            return;
        case 5:
            //流拍
            [self failed];
            return;
        default:
            break;
    }
}

/**
 即将开始
 */
- (void)readyStart {
    self.timeLabel.hidden = false;
    self.endLabel.hidden = true;
    self.timeLabel.text = [NSString stringWithFormat:@"距离开始 %@", @"00: 00: 00"];
    self.timeLabel.backgroundColor = SKColorWithHex(0x888888);
}

/**
 开始
 */
- (void)start {
    self.timeLabel.hidden = false;
    self.endLabel.hidden = true;
    self.timeLabel.text = [NSString stringWithFormat:@"距结束 %@", @"00: 00: 00"];
    self.timeLabel.backgroundColor = SKColorWithHex(0xFF2424);
}

/**
 即将结束
 */
- (void)readyEnd {
    self.timeLabel.hidden = false;
    self.endLabel.hidden = true;
    self.timeLabel.text = [NSString stringWithFormat:@"距结束 %@", @"00: 00: 00"];
    self.timeLabel.backgroundColor = SKColorWithHex(0xFF2424);
}

/**
 结束
 */
- (void)end {
    self.timeLabel.hidden = true;
    self.endLabel.hidden = false;
}
/**
 流拍
 */
- (void)failed {
    self.timeLabel.hidden = true;
    self.endLabel.hidden = false;
    self.endLabel.text = @"已结束";
}
@end
